﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyVideoViewerLibrary;
using System.IO;
using System.Diagnostics;

namespace MyVideoViewerManager
{
    /// <summary>
    /// Interaction logic for TrailersControl.xaml
    /// </summary>
    public partial class TrailersControl : UserControl
    {
        string[] extensions = new string[] { ".mov", ".avi", ".mpeg", ".mpg", ".mkv", ".wmv", ".mp4", ".dvr-ms", ".m2ts", ".ts" };

        public TrailersControl()
        {
            InitializeComponent();
            trailersListBox.SelectionChanged += new SelectionChangedEventHandler(trailersListBox_SelectionChanged);
            rad1.Checked += new RoutedEventHandler(rad1_Checked);
            rad2.Checked += new RoutedEventHandler(rad1_Checked);
            Loaded += new RoutedEventHandler(TrailersControl_Loaded);
        }

        void rad1_Checked(object sender, RoutedEventArgs e)
        {
            LoadTrailers();
        }

        void trailersListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            MovieTrailer trailer = trailersListBox.SelectedValue as MovieTrailer;
            if(trailer == null)
                return;
            trailerGrid.DataContext = trailer;
            genreTextBlock.Text = string.Join(", ", trailer.MovieTrailer_Genres.Select(mg => mg.Genre.GenreDesc).ToArray());
            castTextBlock.Text = string.Join(", ", trailer.MovieTrailer_Persons.Select(mg => mg.Person.PersonName).ToArray());
            trailerGrid.Visibility = Visibility.Visible;

            DirectoryInfo folder = new DirectoryInfo(trailer.MovieFolder);
            if(!folder.Exists)
                return;
            List<FileInfo> files = new List<FileInfo>();
            foreach (FileInfo file in folder.GetFiles().OrderBy(f => f.Name))
            {
                if (extensions.Contains(file.Extension))
                {
                    files.Add(file);
                }
            }
            filesListbox.ItemsSource = files;
        }

        void TrailersControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadTrailers();
        }

        void LoadTrailers()
        {
            if(rad1.IsChecked.Value)
                trailersListBox.ItemsSource = MyVideoViewerBusiness.VideoViewerContext.MovieTrailers.OrderBy(m => m.SortTitle);
            else if (rad2.IsChecked.Value)
                trailersListBox.ItemsSource = MyVideoViewerBusiness.VideoViewerContext.MovieTrailers.OrderByDescending(m => m.PostDate).ThenBy(m => m.SortTitle);
        }

        private void TextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            TextBlock tb = sender as TextBlock;
            if (tb == null)
                return;
            FileInfo file = tb.Tag as FileInfo;
            if (file == null)
                return;
            try
            {
                Process.Start(file.FullName);
            }
            catch(Exception ex)
            {
                string mes = ex.Message;
            }
        }
    }
}
